(function(e){if(e.BX.dataBase)return;var t=e.BX;t.dataBase=function(t){this.tableList=[];this.jsonFields={};if(typeof e.SQLitePlugin!="undefined"&&typeof e.SQLitePlugin.openDatabase=="function"){this.dbObject=e.SQLitePlugin.openDatabase(t);this.dbBandle="SQLitePlugin"}else if(typeof e.openDatabase!="undefined"){this.dbBandle="openDatabase";this.dbObject=e.openDatabase(t.name,t.version,t.displayName,t.capacity)}else{this.dbBandle="undefined";this.dbObject=null}};t.dataBase.create=function(i){if(typeof e.openDatabase!="undefined"||typeof e.SQLitePlugin!="undefined"&&typeof e.SQLitePlugin.openDatabase=="function"){return new t.dataBase(i)}else{return null}};t.dataBase.prototype.setJsonFields=function(e,i){if(typeof i=="string"){if(i==""){i=[]}else{i=[i]}}if(e&&t.type.isArray(i)){e=e.toString().toUpperCase();this.jsonFields[e]=[];if(i.length>0){for(var a=0;a<i.length;a++){this.jsonFields[e].push(i[a].toString().toUpperCase())}}else{delete this.jsonFields[e]}}return true};t.dataBase.prototype.isTableExists=function(e,i){e=e.toUpperCase();var a=new t.Promise;if(typeof i!="function"){i=function(){}}var s=function(t){if(t.indexOf(e)>-1){i(true,e);a.fulfill(e)}else{i(false,e);a.reject(e)}};if(this.tableList.length<=0){this.getTableList().then(s)}else{s(this.tableList)}return a};t.dataBase.prototype.getTableList=function(e){var i=new t.Promise;if(typeof e!="function"){e=function(){}}var a=e;this.query({query:"SELECT tbl_name from sqlite_master WHERE type = 'table'",values:[]}).then(function(e){this.tableList=[];if(e.result.count>0){for(var t=0;t<e.result.items.length;t++){this.tableList[this.tableList.length]=e.result.items[t].tbl_name.toString().toUpperCase()}}a(this.tableList);i.fulfill(this.tableList)}.bind(this)).catch(function(e){i.reject(e)});return i};t.dataBase.prototype.createTable=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="create";this.query(this.getQuery(e)).then(function(t){this.getTableList();t.result.tableName=e.tableName;e.success(t.result,t.transaction);i.fulfill(t)}.bind(this)).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.dropTable=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="drop";this.query(this.getQuery(e)).then(function(t){this.getTableList();t.result.tableName=e.tableName;e.success(t.result,t.transaction);i.fulfill(t)}.bind(this)).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.addRow=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="insert";this.query(this.getQuery(e)).then(function(t){e.success(t.result,t.transaction);t.result.tableName=e.tableName;i.fulfill(t)}).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.replaceRow=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="replace";this.query(this.getQuery(e)).then(function(t){e.success(t.result,t.transaction);t.result.tableName=e.tableName;i.fulfill(t)}).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.getRows=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="select";this.query(this.getQuery(e)).then(function(t){var a=e.tableName.toString().toUpperCase();if(this.jsonFields[a]&&this.jsonFields[a].length&&t.result.items.length){for(var s=0;s<t.result.items.length;s++){for(var r=0;r<this.jsonFields[a].length;r++){if(t.result.items[s][this.jsonFields[a][r]]){t.result.items[s][this.jsonFields[a][r]]=JSON.parse(t.result.items[s][this.jsonFields[a][r]])}}}}e.success(t.result,t.transaction);t.result.tableName=e.tableName;i.fulfill(t)}.bind(this)).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.updateRows=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="update";this.query(this.getQuery(e)).then(function(t){e.success(t.result,t.transaction);t.result.tableName=e.tableName;i.fulfill(t)}).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.deleteRows=function(e){var i=new t.Promise;e=e||{};if(typeof e.success!="function"){e.success=function(e,t){}}if(typeof e.fail!="function"){e.fail=function(e,t,i){}}e.action="delete";this.query(this.getQuery(e)).then(function(t){e.success(t.result,t.transaction);t.result.tableName=e.tableName;i.fulfill(t)}).catch(function(t){e.fail(t.result,t.transaction,t.query,e);t.queryParams=e;i.reject(t)});return i};t.dataBase.prototype.getQuery=function(e){var i=[];var a=e.filter;var s=e.order||null;var r=e.limit||null;var n=e.fields;var o=e.insertFields;var f=e.updateFields;var u=e.tableName;var l="";switch(e.action){case"create":{var c="";if(typeof n=="object"){var p="";var h="";for(var y=0;y<n.length;y++){p="";h="";if(typeof n[y]=="object"){if(n[y].name){p=n[y].name}if(p&&n[y].type){if(n[y].type.toLowerCase()=="integer"||n[y].type.toLowerCase()=="real"||n[y].type.toLowerCase()=="text"){p+=" "+n[y].type}}if(p&&n[y].unique&&n[y].unique==true){p+=" unique"}}else if(typeof n[y]=="string"&&n[y].length>0){p=n[y]}if(p.length>0){if(c.length>0)c+=","+p.toUpperCase();else c=p.toUpperCase()}}}l="CREATE TABLE IF NOT EXISTS "+u.toUpperCase()+" ("+c+") ";break}case"drop":{l="DROP TABLE IF EXISTS "+u.toUpperCase();break}case"select":{var g=[];if(s&&typeof s=="object"){for(var v in s){if(s.hasOwnProperty(v)){g.push(v+" "+(s[v]=="DESC"?"DESC":"ASC"))}}}var d=null;if(r){if(typeof r=="object"){d=parseInt(r.limit)+(r.offset?", "+parseInt(r.offset):"")}else if(typeof r=="number"){d=r}}l="SELECT "+this.getValueArrayString(n,"*")+" FROM "+u.toUpperCase()+" "+this.getFilter(a)+(g.length>0?" ORDER BY "+g.join(", ")+" ":"")+(d?" LIMIT "+d+" ":"");i=this.getValues([a]);break}case"replace":{var b=0;var m=0;var j="";if(t.type.isArray(o)){i=this.getValues(o,"insert");for(var S in o[0]){m++}b=o.length;j=this.getKeyString(o[0])}else{i=this.getValues([o],"insert");b=1;m=i.length;j=this.getKeyString(o)}l="REPLACE INTO "+u.toUpperCase()+" ("+j+") VALUES %values%";var q=[];var B=[];for(var S=0;S<b;S++){q=[];for(var y=0;y<m;y++){q.push("?")}B.push(q.join(","))}l=l.replace("%values%","("+B.join("), (")+")");break}case"insert":{var b=0;var m=0;var j="";if(t.type.isArray(o)){i=this.getValues(o,"insert");for(var S in o[0]){m++}b=o.length;j=this.getKeyString(o[0])}else{i=this.getValues([o],"insert");b=1;m=i.length;j=this.getKeyString(o)}l="INSERT INTO "+u.toUpperCase()+" ("+j+") VALUES %values%";var q=[];var B=[];for(var S=0;S<b;S++){q=[];for(var y=0;y<m;y++){q.push("?")}B.push(q.join(","))}l=l.replace("%values%","("+B.join("), (")+")");break}case"delete":{l="DELETE FROM "+u.toUpperCase()+" "+this.getFilter(a);i=this.getValues([a]);break}case"update":{l="UPDATE "+u.toUpperCase()+" "+this.getFieldPair(f,"SET ")+" "+this.getFilter(a);i=this.getValues([f],"update").concat(this.getValues([a]));break}}return{query:l,values:i}};t.dataBase.prototype.getFieldPair=function(e,t){var i="";var a=t||"";if(typeof e=="object"){var s=0;for(var r in e){var n=(s>0?", ":"")+(r.toUpperCase()+"="+"?");if(i.length==0&&a.length>0)i=a;i+=n;s++}}return i};t.dataBase.prototype.getFilter=function(e){var t="";var i="WHERE ";if(typeof e=="object"){var a=0;for(var s in e){var r="";var n=1;if(typeof e[s]=="object"){n=e[s].length}for(var o=0;o<n;o++){r=(o>0?r+" OR ":"(")+(s.toUpperCase()+"="+"?");if(o+1==n)r+=")"}t+=r;a++}}else if(typeof e=="string"){t=e}return t==""?"":"WHERE "+t};t.dataBase.prototype.getKeyString=function(e,i){var a="";if(!i)i="";if(t.type.isArray(e)){for(var s=0;s<e.length;s++){for(var r in e[s]){if(a.length>0)a+=","+r.toUpperCase();else a=r.toUpperCase()}}}else if(typeof e=="object"){for(var r in e){if(a.length>0)a+=","+r.toUpperCase();else a=r.toUpperCase()}}if(a.length==0)a=i;return a};t.dataBase.prototype.getValueArrayString=function(e,t){var i="";if(!t)t="";if(typeof e=="object"){for(var a=0;a<e.length;a++){if(i.length>0)i+=","+e[a].toUpperCase();else i=e[a].toUpperCase()}}if(i.length==0)i=t;return i};t.dataBase.prototype.getValues=function(e,i){i=i||"undefined";var a=[];for(var s=0;s<e.length;s++){var r=e[s];if(t.type.isArray(r)){for(var n=0;n<r.length;n++){if((i=="insert"||i=="update")&&typeof r[n]=="object"){a.push(JSON.stringify(r[n]))}else if(typeof r[n]=="object"){for(var o in r[n]){if(typeof r[n][o]=="object"){a.push(JSON.stringify(r[n][o]))}else{a.push(r[n][o])}}}else{a.push(r[n])}}}else if(typeof r=="object"){for(var n in r){if((i=="insert"||i=="update")&&typeof r[n]=="object"){a.push(JSON.stringify(r[n]))}else if(typeof r[n]=="object"){for(var o in r[n]){if(typeof r[n][o]=="object"){a.push(JSON.stringify(r[n][o]))}else{a.push(r[n][o])}}}else{a.push(r[n])}}}}return a};t.dataBase.prototype.query=function(e,i,a){var s=new t.Promise;if(typeof i!="function"){i=function(e,t){}}if(typeof a!="function"){a=function(e,t,i){}}if(!this.dbObject){a(null,null,null);s.reject(null,null,null);return s}this.dbObject.transaction(function(t){t.executeSql(e.query,e.values,function(e,t){var a={originalResult:t};var r=t.rows.length;if(r>=0){a.count=r;a.items=[];for(var n=0;n<r;n++){var o={};var f=t.rows.item(n);for(var u in f){if(f.hasOwnProperty(u)){o[u]=f[u]}}a.items.push(o)}}i(a,e);s.fulfill({result:a,transaction:e})},function(t,i){a(i,t,e);s.reject({result:i,transaction:t,query:e})})},function(e){console.error("BX.dataBase.prototype.query: ",e);s.reject(null,null,null)});return s};t.dataBase.prototype.getResponseObject=function(e){var t=e.rows.length;var i=[];for(var a=0;a<t;a++){i[i.length]=e.rows.item(a)}return i}})(window);